package com.glxt.controller;

import com.glxt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Calendar;

@Controller
public class LoginController {
    @Autowired
    private UserService userServiceImpl;
    @PostMapping("/gologin")
    public String gologin(@RequestParam("name") String username, @RequestParam("password") String userpass, HttpSession session){
        if(userServiceImpl.login(username,userpass) != null){
//        if("admin".equals(username) && "123".equals(userpass)){
            session.setAttribute("currentuser", username);
            System.out.println(username);
            return "index";
        }
        else {
            return "error";
        }
    }
    @GetMapping("/login")
    public String login(Model model){
        model.addAttribute("currentYear", Calendar.getInstance().get(Calendar.YEAR));
        return "login";
    }
    @GetMapping("/register")
    public String register(){
        return "register";
    }
    @GetMapping("/index")
    public String index(){
        return "index";
    }
    @GetMapping("/publicpage")
    public String publicpage(){
        return "publicpage";
    }

}
